#include "pileaffichage.h"

PileAffichage::PileAffichage(QTextEdit *p):afficheur(p)
{
    afficheur->setPlainText("");
    text = nb = "";
}

QString PileAffichage::addText(const QString s){
    text += nb + s;
    nb = "";
    updateAfficheur();
    return text;
}
QString PileAffichage::setText(const QString s){
    if (s == "0")
        nb = "0";
    else
        nb = s;
    updateAfficheur();
    return text;
}
QString PileAffichage::getText(){
    return text+nb;
}

void PileAffichage::iniText(){
    text = nb = "";
    updateAfficheur();
}
void PileAffichage::pushText(){
    push(text);
    iniText();
    updateAfficheur();
}
void PileAffichage::push(const QString &t){
    QStack::push(t);
    updateAfficheur();
}
void PileAffichage::updateAfficheur() const{
    afficheur->setPlainText(historique()+text+nb);
    afficheur->setAlignment(Qt::AlignRight);
}
QString PileAffichage::historique()const {
    QString h = "";
    QStack<QString>::const_iterator i;
    for (i = begin(); i != end(); ++i)
        h += *i + "\n";
    return h+"";
}

void PileAffichage::clear(){
    QStack<QString>::clear();
    iniText();
}

void PileAffichage::sauvegarder(){
    QSettings settings("mesOptions.ini", QSettings::IniFormat);
    int taille = this->size();
    settings.beginGroup("PileAffichage");
    settings.setValue("text", text);
    settings.setValue("taille", taille);
    settings.beginWriteArray("Valeur");
    QString *data = this->data();
    for(int i=0; i!=taille; i++){
       settings.setArrayIndex(i);
       settings.setValue("Affichage", data[i]);
   }
    settings.endArray();
    settings.endGroup();


}
